package com.example.springcloudwinter.mapper;

import com.example.springcloudwinter.bo.AddressBo;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

@Mapper
public interface DeliveryAddressMapper {
    
    @Update ("UPDATE deliveryaddress SET contactName=#{contactName},contactSex=#{contactSex}," +
            "contactTel=#{contactTel},address=#{address} WHERE userId=#{userId} AND daId=#{addressID}")
    void changeAddress (@Param ("contactName") String name, @Param ("contactSex") int sex, @Param ("contactTel")
            String phone, @Param ("address") String address, @Param ("userId") String userId, @Param ("addressID") int addressId);
    
    @Select ("SELECT daId addressId,contactName name,contactSex sex,contactTel phone,address,userId FROM deliveryaddress WHERE userId=#{userId}")
    List<AddressBo> getAddressByUserId (@Param ("userId") String userId) throws SQLException;
    
    @Insert ("INSERT INTO deliveryaddress (userId,contactName,address,contactTel,contactSex) VALUES (#{userId},#{name},#{address},#{phone}" +
            ",#{sex})")
    int saveDeliveryAddress (@Param ("userId") String userId, @Param ("name") String name, @Param ("address") String address,
                             @Param ("phone") String phone, @Param ("sex") int sex) throws SQLException;
    
    @Select ("SELECT daId addressId,contactName name,contactSex sex,contactTel phone,address,userId" +
            " FROM deliveryaddress WHERE daId=#{addressId} AND userId=#{userId}")
    AddressBo getDeliveryAddressByAddressId (@Param ("addressId") int addressId, @Param ("userId") String userId) throws SQLException;
    
    @Delete ("DELETE FROM deliveryaddress WHERE daId=#{addressId} AND userId=#{userId}")
    int removeDeliveryAddress (@Param ("addressId") int addressId, @Param ("userId") String userId) throws SQLException;
}
